using System;
using System.Runtime.InteropServices;
using ScenarioFramework.WorkflowEngine.DataModel.WorkflowApplicationsData;

namespace ScenarioFramework.WAPI
{
	/// <summary>
	/// Descrizione di riepilogo per wrpWAPIRequest.
	/// </summary>
	public class Application
	{
		/// <summary>
		/// Initializes a new instance  of the <see cref="Application"/> class.
		/// </summary>
		public Application()
		{
		}

		/// <summary>
		/// Executes the API.
		/// </summary>
		/// <param name="sApplicationApi">The s application API.</param>
		/// <param name="response">The response.</param>
		/// <returns></returns>
		[ DllImport( "WAPI.dll", EntryPoint="executeApi")]
		private static extern String ExecuteApi( String sApplicationApi, ref Response response );

		/// <summary>
		/// Gets the application for extended attribute.
		/// </summary>
		/// <param name="AttributeName">Name of the attribute.</param>
		/// <param name="AttributeValue">The attribute value.</param>
		/// <param name="response">The response.</param>
		/// <returns></returns>
		[ DllImport( "WAPI.dll", EntryPoint="getApplicationForExtendedAttribute")]
		private static extern String GetApplicationForExtendedAttribute(String AttributeName, String AttributeValue, ref Response response);

		/// <summary>
		/// Gets the applications.
		/// </summary>
		/// <param name="response">The response.</param>
		/// <returns></returns>
		[ DllImport( "WAPI.dll", EntryPoint="getApplications")]
		private static extern String GetApplications(ref Response response);

		/// <summary>
		/// Runs all applications.
		/// </summary>
		/// <param name="response">The response.</param>
		private static void RunAllApplications(ref Response response)
		{
			Response wapiResponse = new Response();
			string sXML = Application.GetApplications(ref wapiResponse );

			if ( wapiResponse.iCode != 0 ) 
			{
				return;
			}

			XMLApplications xmlApplications = new XMLApplications();
			xmlApplications.Parse (sXML);

			XMLApplication xmlApplication = null;
			string sFile = Environment.GetEnvironmentVariable("WF_HOME") + @"\bin\";
			for ( int i=0; i<xmlApplications.Count;i++)
			{
				xmlApplication = (XMLApplication)xmlApplications[i];

				if ( xmlApplication.Type == XMLApplication.EXECUTABLE_TYPE )
				{
					bOSdotNET.Microsoft.Windows.ProcessHandler.Start (xmlApplication.LibraryFile, sFile + xmlApplication.LibraryFile + @".EXE", sFile, true);
				}
			}		
		}

		/// <summary>
		/// Runs all applications.
		/// </summary>
		/// <param name="xmlApplications">The XML applications.</param>
		private static void RunAllApplications(XMLApplications xmlApplications)
		{
			XMLApplication xmlApplication = null;
			string sFile = Environment.GetEnvironmentVariable("WF_HOME") + @"\bin\";
			for ( int i=0; i<xmlApplications.Count;i++)
			{
				xmlApplication = (XMLApplication)xmlApplications[i];

				if ( xmlApplication.Type == XMLApplication.EXECUTABLE_TYPE )
				{
					bOSdotNET.Microsoft.Windows.ProcessHandler.Start (xmlApplication.LibraryFile, sFile + xmlApplication.LibraryFile + @".EXE", sFile, true);
				}
			}		
		}
	}
}
